package com.polylink.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.polylink.base.Page;
import com.polylink.sys.bean.Model;
import com.polylink.sys.dao.IModelDao;
import com.polylink.sys.service.IModelService;

/**
 *@author xiliang.xiao
 *@date 2015年3月23日 上午9:03:48
 *
 **/
@Service
public class ModelService implements IModelService{
	@Autowired
	private IModelDao modelDao;

	@Override
	public boolean save(Model model) {
		int i = 0;
		if(model.getId()!=null){
			i = modelDao.update(model);
		}else{
			i = modelDao.add(model);
		}
		if(i>0){
			return true;
		}
		return false;
	}

	@Override
	public Page list(Page p) {
		List<Model> list = modelDao.list(p);
		int count = modelDao.count(p);
		p.setRows(formatteList(list));
		p.setTotal(count);
		return p;
	}

	private List<?> formatteList(List<Model> list) {
		if(list!=null&&list.size()>0){
			List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
			for(Model model:list){
				Map<String,Object> map =new HashMap<String, Object>();
				map.put("id", model.getId());
				map.put("name", model.getName());
				map.put("remark", model.getRemark());
				map.put("_parentId", model.getParentId());
				data.add(map);
			}
			return data;
		}
		return null;
	}

	@Override
	public boolean delete(String id) {
		int i = modelDao.delete(id);
		if(i>0){
			return true;
		}
		return false;
	}

	@Override
	public List<Model> select() {
		return modelDao.select();
	}

}
